<?php

/**
 * Created by PhpStorm.
 * User: SongKejing
 * QQ: 597481334
 * Date: 2018/2/24
 * Time: 14:28
 */

namespace ClassLibrary;

/**
 * 字段基础定义
 * Class ClFieldBase
 * @package ClassLibrary
 */
class ClFieldBase extends ClBase {

    /**
     * 字段配置
     * @var array
     */
    protected $field_config = [];

    /**
     * 格式化
     * @param string $message
     * @return string
     */
    private function formatMsg($message = '') {
        if (!empty($message)) {
            $message = ':' . $message;
        }
        return $message;
    }

    /**
     * 过滤器
     * @param array $filters 例如，['trim', 'intval']
     * @return $this
     */
    public function filters($filters = ['trim']) {
        $this->field_config['filters'] = ClArray::itemFilters($filters);
        return $this;
    }

    /**
     * 必须填写
     * @param string $message
     * @return $this
     */
    public function verifyIsRequire($message = '') {
        if (!isset($this->field_config['verifies'])) {
            $this->field_config['verifies'][] = 'is_required' . $this->formatMsg($message);
        } else {
            if (!in_array('is_required', $this->field_config['verifies'])) {
                $this->field_config['verifies'][] = 'is_required' . $this->formatMsg($message);
            }
        }
        return $this;
    }

    /**
     * 是否是密码
     * @param int $min
     * @param int $max
     * @param string $message
     * @return $this
     */
    public function verifyIsPassword($min = 6, $max = 18, $message = '') {
        $this->field_config['verifies'][] = ['password' . $this->formatMsg($message), intval($min), intval($max)];
        return $this;
    }

    /**
     * 是否在数组内
     * @param array $valid_values
     * @param string $message
     * @return $this
     */
    public function verifyInArray($valid_values = [], $message = '') {
        $this->field_config['verifies'][] = ['in_array' . $this->formatMsg($message), ClArray::itemFilters($valid_values)];
        return $this;
    }

    /**
     * 是否在范围内
     * @param $min
     * @param $max
     * @param string $message
     * @return $this
     */
    public function verifyIntInScope($min, $max, $message = '') {
        $this->field_config['verifies'][] = ['in_scope' . $this->formatMsg($message), intval($min), intval($max)];
        return $this;
    }

    /**
     * 最大
     * @param $max
     * @param string $message
     * @return $this
     */
    public function verifyIntMax($max, $message = '') {
        $this->field_config['verifies'][] = ['max' . $this->formatMsg($message), intval($max)];
        return $this;
    }

    /**
     * 最小
     * @param $min
     * @param string $message
     * @return $this
     */
    public function verifyIntMin($min, $message = '') {
        $this->field_config['verifies'][] = ['min' . $this->formatMsg($message), intval($min)];
        return $this;
    }

    /**
     * 字符串长度
     * @param $length
     * @param string $message
     * @return $this
     */
    public function verifyStringLength($length, $message = '') {
        $this->field_config['verifies'][] = ['length' . $this->formatMsg($message), intval($length)];
        return $this;
    }

    /**
     * 字符串最长
     * @param $length
     * @param string $message
     * @return $this
     */
    public function verifyStringLengthMax($length, $message = '') {
        $this->field_config['verifies'][] = ['length_max' . $this->formatMsg($message), intval($length)];
        return $this;
    }

    /**
     * 字符串最短
     * @param $length
     * @param string $message
     * @return $this
     */
    public function verifyStringLengthMin($length, $message = '') {
        $this->field_config['verifies'][] = ['length_min' . $this->formatMsg($message), intval($length)];
        return $this;
    }

    /**
     * 邮件
     * @param string $message
     * @return $this
     */
    public function verifyEmail($message = '') {
        $this->field_config['verifies'][] = 'email' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 手机
     * @param string $message
     * @return $this
     */
    public function verifyMobile($message = '') {
        $this->field_config['verifies'][] = 'mobile' . $this->formatMsg($message);
        return $this;
    }

    /**
     * ip地址
     * @param string $message
     * @return $this
     */
    public function verifyIp($message = '') {
        $this->field_config['verifies'][] = 'ip' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 邮政编码校验
     * @param string $message
     * @return $this
     */
    public function verifyPostcode($message = '') {
        $this->field_config['verifies'][] = 'postcode' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 身份证
     * @param string $message
     * @return $this
     */
    public function verifyIdCard($message = '') {
        $this->field_config['verifies'][] = 'id_card' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 汉字
     * @param string $message
     * @return $this
     */
    public function verifyChinese($message = '') {
        $this->field_config['verifies'][] = 'chinese' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 汉字、字母
     * @param string $message
     * @return $this
     */
    public function verifyChineseAlpha($message = '') {
        $this->field_config['verifies'][] = 'chinese_alpha' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 汉字、字母、数字
     * @param string $message
     * @return $this
     */
    public function verifyChineseAlphaNum($message = '') {
        $this->field_config['verifies'][] = 'chinese_alpha_num' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 汉字、字母、数字、下划线_、破折号-
     * @param string $message
     * @return $this
     */
    public function verifyChineseAlphaNumDash($message = '') {
        $this->field_config['verifies'][] = 'chinese_alpha_num_dash' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 字母
     * @param string $message
     * @return $this
     */
    public function verifyAlpha($message = '') {
        $this->field_config['verifies'][] = 'alpha' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 字母和数字
     * @param string $message
     * @return $this
     */
    public function verifyAlphaNum($message = '') {
        $this->field_config['verifies'][] = 'alpha_num' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 字母、数字，下划线_、破折号-
     * @param string $message
     * @return $this
     */
    public function verifyAlphaNumDash($message = '') {
        $this->field_config['verifies'][] = 'alpha_num_dash' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 网址
     * @param string $message
     * @return $this
     */
    public function verifyUrl($message = '') {
        $this->field_config['verifies'][] = 'url' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 数字
     * @param string $message
     * @return $this
     */
    public function verifyNumber($message = '') {
        $this->field_config['verifies'][] = 'number' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 数组
     * @param string $message
     * @return $this
     */
    public function verifyArray($message = '') {
        $this->field_config['verifies'][] = 'array' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 固话
     * @param string $message
     * @return $this
     */
    public function verifyTel($message = '') {
        $this->field_config['verifies'][] = 'tel' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 时间格式
     * @param string $message
     * @return $this
     */
    public function verifyIsDate($message = '') {
        $this->field_config['verifies'][] = 'is_date' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 唯一值
     * @param string $message
     * @return $this
     */
    public function verifyUnique($message = '') {
        $this->field_config['verifies'][] = 'unique' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 是否是域名
     * @param string $message
     * @return $this
     */
    public function verifyIsDomain($message = '') {
        $this->field_config['verifies'][] = 'is_domain' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 是否是json格式
     * @param string $message
     * @return $this
     */
    public function verifyIsJson($message = '') {
        $this->field_config['verifies'][] = 'is_json' . $this->formatMsg($message);
        return $this;
    }

    /**
     * 合并字段配置
     * @param array $field_config
     * @return $this
     */
    public function verifyMergeConfig($field_config = []) {
        if (!isset($this->field_config['verifies'])) {
            $this->field_config['verifies'] = [];
        }
        if (is_null($field_config)) {
            //兼容处理
            $field_config = [];
        }
        $this->field_config['verifies'] = array_merge($this->field_config['verifies'], $field_config);
        return $this;
    }
}
